package cn.com.huzo.service.manufacture.impl;

import cn.com.huzo.model.entities.manufacture.CmtSettlementSlipHeaderEntity;
import huzofw.model.entities.paginator.Paginator;
import huzofw.service.impl.GenericManagerImpl;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import cn.com.huzo.repository.manufacture.CmtSettlementSlipHeaderRepository;
import cn.com.huzo.repository.manufacture.CmtSettlementSlipLinesRepository;
import cn.com.huzo.service.manufacture.CmtSettlementSlipHeaderManager;
import org.apache.commons.collections4.MapUtils;

/**
 * 加工费计算信息管理服务对象类实现。
 *
 * @author 吴旭
 */
@Service("CmtSettlementSlipHeaderManager")
public class CmtSettlementSlipHeaderManagerImpl extends GenericManagerImpl<CmtSettlementSlipHeaderEntity, Long>
        implements CmtSettlementSlipHeaderManager {

    @Resource(name = "CmtSettlementSlipHeaderRepository")
    private CmtSettlementSlipHeaderRepository _dao_header;

    @Resource(name = "CmtSettlementSlipLinesRepository")
    private CmtSettlementSlipLinesRepository _dao_lines;

    /**
     * {@inheritDoc}
     *
     * @throws java.lang.Exception
     */
    @Override
    @Transactional
    public Map<String, Object> fetchByHeaderKey(Long headerId) throws Exception {
        Map<String, Object> _result = new HashMap();
        // 得到表头信息
        _result.put("HeaderEntity", _dao_header.fetchByKey(headerId));
        _result.put("LinesList", _dao_lines.fetchByHeaderId(headerId));

        return _result;
    }

    /**
     * {@inheritDoc}
     *
     * @throws java.lang.Exception
     */
    @Transactional
    @Override
    public Map<String, Object> fetchForPage(Map<String, Object> criterion) throws Exception {
        Map<String, Object> _result = new HashMap();

        Long offset = (long) (MapUtils.getInteger(criterion, "Limit") * (MapUtils.getInteger(criterion, "PageIndex") - 1));
        long total = _dao_header.fetchForCount(criterion);

        if (total > 0) {
            if (offset >= total) {
                offset = (total - MapUtils.getInteger(criterion, "Limit")) < 0 ? 0 : (total - MapUtils.getInteger(criterion, "Limit"));
            }
            criterion.put("Offset", offset);
            List<CmtSettlementSlipHeaderEntity> itemList = _dao_header.fetchForPage(criterion);
            Paginator<CmtSettlementSlipHeaderEntity> paginator = new Paginator<>(itemList, offset, MapUtils.getInteger(criterion, "Limit"), total);

            _result.put("TotalRecordCount", paginator.getTotal());
            _result.put("TotalPages", paginator.getTotalPages());
            _result.put("PageIndex", paginator.getPageIndex());
            _result.put("DataList", paginator.getItemList());
            _result.put("Success", true);
        } else {
            _result.put("Success", false);
        }

        return _result;
    }

    /**
     * {@inheritDoc}
     *
     * @throws java.lang.Exception
     */
    @Override
    public String fetchNewBillNo() throws Exception {
        return _dao_header.fetchNewBillNo();
    }

    /**
     * {@inheritDoc}
     *
     * @throws java.lang.Exception
     */
    @Transactional
    @Override
    public Long insert(CmtSettlementSlipHeaderEntity instance) throws Exception {
        Long _headerId = _dao_header.insert(instance);
        instance.setHeaderId(_headerId);
        _dao_lines.insertLinesByNormal(instance);   //插入明细
        return _headerId;
    }

    /**
     * {@inheritDoc}
     *
     * @throws java.lang.Exception
     */
    @Transactional
    @Override
    public Integer update(CmtSettlementSlipHeaderEntity instance) throws Exception {
        return _dao_header.update(instance);
    }

    /**
     * {@inheritDoc}
     *
     * @throws java.lang.Exception
     */
    @Transactional
    @Override
    public Integer delete(CmtSettlementSlipHeaderEntity instance) throws Exception {
        return _dao_header.delete(instance.getHeaderId());
    }
}
